package com.neusoft.controller;

import com.neusoft.entity.Pay;
import com.neusoft.entity.PayMoney;
import com.neusoft.entity.Rent;
import com.neusoft.entity.common.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;
import java.util.Arrays;

@Controller
@RequestMapping("payConsumer")
public class PayConsumerController {
    @Autowired
    private RestTemplate restTemplate;

    private String url_prefix = "http://localhost:8001";
//      private String url_prefix = "http://provider-neusoft";

    @PostMapping("save")
    @ResponseBody
    public void save(Pay bean){
        System.out.println("修改或添加的对象为："+bean);
        String url = url_prefix + "/pay/save";
        restTemplate.put(url,bean);
    }

    @PostMapping("queryAll")
    @ResponseBody
    public Object queryAll(Integer page, Integer limit,Pay bean) {
        System.out.println("bean值为："+bean);
        System.out.println("page值为："+page);
        System.out.println("limit值为："+limit);
        String url = url_prefix + "/pay/queryAll/"+page+"/"+limit;
        return restTemplate.postForObject(url,bean, CommonResult.class);
    }

    @RequestMapping("queryById/{id}")
    @ResponseBody
    public Object queryById(@PathVariable Integer id) {
        String url = url_prefix + "/pay/queryById/" + id;
        return restTemplate.getForObject(url, Pay.class);
    }

    @PostMapping("deleteById")
    @ResponseBody
    public ResponseEntity<Boolean> deleteById(Integer[] ids){
        for(int i=0;i<ids.length;i++) {
            System.out.println("ids值为：" + ids[i]);
        }
        StringBuffer sb = new StringBuffer();
        System.out.println(Arrays.toString(ids));
        sb.append(Arrays.toString(ids));
        sb.deleteCharAt(sb.indexOf("["));
        sb.deleteCharAt(sb.lastIndexOf("]"));
        System.out.println(sb.toString());
        String url = url_prefix+"/pay/deleteById/" + sb.toString();
        return restTemplate.postForEntity(url,sb.toString(),Boolean.class);
    }

    @PostMapping("deleteById1/{id}")
    @ResponseBody
    public ResponseEntity<Boolean> deleteById1(@PathVariable Integer id){
        String url = url_prefix+"/pay/deleteById1/" + id;
        return restTemplate.postForEntity(url,id,Boolean.class);
    }

    @RequestMapping("toList")
    public String toList() {
        return "user/pay_list";
    }

    @RequestMapping("toEdit")
    public String toEdit(Integer id, Model model){
        Pay bean = (Pay) queryById(id);
        model.addAttribute("bean",bean);
        return "user/pay_add";
    }

    @RequestMapping("toAdd")
    public String toAdd(Model model){
        model.addAttribute("bean",new Pay());
        return "user/pay_add";
    }

    @RequestMapping("toPay")
    public String toPay(Integer id, Model model){
        Pay pay = (Pay) queryById(id);
        PayMoney payMoney = new PayMoney();
        payMoney.setPayid(id);
        payMoney.setTatal(pay.getTotal());
        boolean result = insert(payMoney);
        PayMoney bean = (PayMoney) queryByPayId(id);
        model.addAttribute("bean",bean);
        return "user/form-step";
    }

    @PostMapping("pay")
    @ResponseBody
    public boolean pay(PayMoney bean){
        System.out.println("确认入款对象为："+bean);
        String url = url_prefix + "/pay/payMoney";
        restTemplate.put(url,bean);
        return true;
    }

    @RequestMapping("queryByPayId/{payId}")
    @ResponseBody
    public Object queryByPayId(@PathVariable Integer payId) {
        String url = url_prefix + "/pay/queryByPayId/" + payId;
        return restTemplate.getForObject(url, PayMoney.class);
    }

    @RequestMapping("insert")
    @ResponseBody
    public boolean insert(PayMoney bean) {
        System.out.println("bean对象为："+bean);
        String uri = url_prefix + "/pay/insert";
        return restTemplate.postForObject(uri, bean,boolean.class);
    }
}
